package com.sc.sysconfig.redis;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sc.sysconfig.entity.SysLog;
import com.sc.sysconfig.entity.SysUser;
import com.sc.sysconfig.service.SysLogService;
import com.sc.sysconfig.service.SysUserService;
import com.sc.sysconfig.util.Token;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * redis订阅
 *
 * @author 徐赫阳
 * @date 2021/09/13
 */
@Component
public class RedisListener {

    @Autowired
    SysUserService sysUserService;
    @Autowired
    SysLogService sysLogService;

    public void getMessage(String str) throws Exception{
        Map map = JSON.parseObject(str,Map.class);
        String token = (String)map.get("token");
        String payload = Token.validToken(token);
        JSONObject object = JSON.parseObject(payload);
        SysUser user = sysUserService.getById((Integer)object.get("userId"));
        SysLog log = new SysLog((String)map.get("url"),user.getName());
        sysLogService.save(log);
    }
}
